package com.neusoft.yuejian12.yuejian_12.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.activity.ActivityDetailActivity;
import com.neusoft.yuejian12.yuejian_12.javabean.ActivityBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.neusoft.yuejian12.yuejian_12.constant.Constant.ACTIVITY_PHOTO_URL;

/**
 * Created by ZTY on 2017/10/3.
 */

public class ActivityListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context mContext;
    private List mDataList;

    private LayoutInflater mLayoutInflatera;
    public ActivityListAdapter(Context mContext,List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflatera = LayoutInflater.from(mContext);
    }
    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflatera.from(parent.getContext())
                .inflate(R.layout.cardview_activity, parent, false);
        return new ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final ActivityBean entity = (ActivityBean) mDataList.get(position);
        if (null == entity)
            return;
        //绑定数据
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.activityname.setText(entity.getA_name());
        String activitypic = ACTIVITY_PHOTO_URL+entity.getA_picture();

        Picasso.with(mContext).load(activitypic).into(viewHolder.imageactivity);
        if (viewHolder.imageactivity.getDrawable()==null){
            viewHolder.imageactivity.setImageResource(R.drawable.ic_add);
        }
        //点击item跳转
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("a_id",entity.getA_id());
                bundle.putString("a_name",entity.getA_name());
                bundle.putInt("a_price",entity.getA_price());
                bundle.putString("a_address",entity.getA_address());
                bundle.putString("a_time",entity.getA_time());
                bundle.putString("a_picture",entity.getA_picture());
                bundle.putString("a_phone",entity.getA_phone());
                Intent intent=new Intent(mContext, ActivityDetailActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);

            }
        });

    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageactivity;
        TextView activityname;

        public ViewHolder(View itemView){
            super(itemView);
            activityname = (TextView) itemView.findViewById(R.id.activity_name);
            imageactivity = (ImageView) itemView.findViewById(R.id.activity_pic);
        }
    }
}
